const { contextBridge, ipcRenderer } = require('electron')

contextBridge.exposeInMainWorld('versions', {
  node: () => process.versions.node,
  chrome: () => process.versions.chrome,
  electron: () => process.versions.electron,
})

let websocket = null;
// ipcRenderer.on('websocket', (event, param) => {
//    websocket = connectWebsocket(param.token, param.userId);
//   // 将接收到的变量暴露给渲染进程
//   contextBridge.exposeInMainWorld('websocketClient', websocket);
// });

contextBridge.exposeInMainWorld('user', {
  //推荐使用invoke
  //UI线程 ——> Main线程
  loginCallback: (e) => ipcRenderer.invoke('login',e),
  logoutCallback: () => ipcRenderer.invoke('logout'),

  //验证token
  onVerifyToken: (callback) => {
    const listener = (_) => callback();
    ipcRenderer.on('verify-token', listener);
    return () => ipcRenderer.removeListener('verify-token', listener);
  },
})


contextBridge.exposeInMainWorld('websocketClient', {
  //UI线程 ——> Main线程
  send: (e) => ipcRenderer.invoke('send-message',e),
})
